﻿namespace LoggingBot
{
    using CustomCharacter;
    using Exiled.API.Enums;
    using Exiled.API.Features;
    using Exiled.Events.EventArgs.Player;
    using Exiled.Events.EventArgs.Server;
    using Exiled.Events.EventArgs.Warhead;
    using RgCore;
    using RgCore.API;
    using RgCore.Modules;
    using static LoggingBot.Logging;

    public class EventHandlers
    {
        public static void RegisterEvents()
        {
            Exiled.Events.Handlers.Player.Died += Player_Died;
            Exiled.Events.Handlers.Player.Verified += Player_Verified;
            Exiled.Events.Handlers.Player.Destroying += Player_Left;
            Exiled.Events.Handlers.Player.Kicking += Player_Kicking;
            Exiled.Events.Handlers.Player.Banning += Player_Banning;
            Exiled.Events.Handlers.Player.ThrownProjectile += Player_ThrowingItem;
            Exiled.Events.Handlers.Server.WaitingForPlayers += Server_WaitingForPlayers;
            RgCore.RoundStarted += Server_RoundStarted;
            Exiled.Events.Handlers.Server.RoundEnded += Server_RoundEnded;
            Exiled.Events.Handlers.Warhead.Starting += Warhead_Starting;
            Exiled.Events.Handlers.Warhead.Stopping += Warhead_Stopping;
            Exiled.Events.Handlers.Warhead.Detonated += Warhead_Detonated;
            Exiled.Events.Handlers.Server.RespawningTeam += Server_RespawningTeam;
        }

        private static string GetCustomRoleName(Player player)
        {
            if (player.IsCustomCharacter())
                return player.GetCharacterInstance().Attribute.Name;
            else
                return player.Role.Name;
        }

        private static string GetRoleName(Player player)
        {
            if (ModuleManager.IsLoaded("CustomCharacter"))
            {
                return GetCustomRoleName(player);
            }
            else
            {
                return player.Role.Name;
            }
        }

        private static void Server_RespawningTeam(RespawningTeamEventArgs ev)
        {
            string team;
            if (ev.NextKnownTeam == Respawning.SpawnableTeamType.NineTailedFox)
            {
                team = "九尾狐";
            }
            else
            {
                team = "混沌";
            }

            SendMessage($"[{team}增援]");
        }

        private static void Warhead_Detonated()
        {
            SendMessage("[核弹爆炸]");
        }

        private static void Warhead_Stopping(StoppingEventArgs ev)
        {
            SendMessage("[核弹关闭] " + "#" + "关闭者: " + ev.Player.Nickname + "#" + $"({ev.Player.UserId})" + "#" + " 玩家身份: " + GetRoleName(ev.Player));
        }

        private static void Warhead_Starting(StartingEventArgs ev)
        {
            SendMessage("[核弹启动] " + "#" + "启动者: " + "#" + ev.Player.Nickname + "#" + $"({ev.Player.UserId})" + "#" + " 玩家身份: " + GetRoleName(ev.Player));
        }

        private static void Player_Banning(BanningEventArgs ev)
        {
            SendMessage("[玩家封禁] " + "#" + "被封禁者: " + "#" + ev.Target.Nickname + "#" + $"({ev.Target.UserId})" + "#" + " 封禁者: " + ev.Player.Nickname + "#" + $"({ev.Player.UserId})" + "#" + " 封禁时长: " + ev.Duration + "#" + " 原因: " + ev.Reason);
        }

        private static void Player_Kicking(KickingEventArgs ev)
        {
            SendMessage("[玩家踢出] " + "#" + "被封禁者: " + "#" + ev.Target.Nickname + "#" + $"({ev.Target.UserId})" + "#" + " 原因: " + ev.Reason);
        }

        private static void Player_ThrowingItem(ThrownProjectileEventArgs ev)
        {
            if (ev.Projectile.ProjectileType == ProjectileType.FragGrenade || ev.Projectile.ProjectileType == ProjectileType.Scp018)
            {
                SendMessage("[丢出投掷物] " + "#" + "投掷者: " + "#" + ev.Player.Nickname + "#" + $"({ev.Player.UserId})" + "#" + " 投掷物品: " + ev.Projectile.Type);
            }
        }

        private static void Player_Verified(VerifiedEventArgs ev)
        {
            SendMessage("[玩家加入] " + "#" + ev.Player.Nickname + "#" + $"({ev.Player.UserId})");
        }

        private static void Player_Left(DestroyingEventArgs ev)
        {
            SendMessage("[玩家离开] " + "#" + ev.Player.Nickname + "#" + $"({ev.Player.UserId})");
        }

        private static void Server_WaitingForPlayers()
        {
            SendMessage("正在等待玩家中");
        }

        private static void Server_RoundStarted()
        {
            SendMessage("回合开始");
        }

        private static void Server_RoundEnded(RoundEndedEventArgs ev)
        {
            SendMessage("回合结束");
        }

        private static void Player_Died(DiedEventArgs ev)
        {
            string killer = "击杀者: " + "#";
            if (ev.Attacker != null)
            {
                killer += ev.Attacker.Nickname + $"({ev.Attacker.UserId})" + "#" + $" 玩家身份: {GetRoleName(ev.Attacker)}";
            }
            else
            {
                killer += "服务器";
            }

            SendMessage("[玩家死亡] " + "#" + killer + "#" + " 被击杀玩家: " + "#" + ev.Player.Nickname + $"({ev.Player.UserId}) " + "#" + "玩家身份: " + ev.TargetOldRole.GetRoleName() + "#" + " 死于: " + ev.DamageHandler.Type);
        }

        public static void UnRegisterEvents()
        {
            Exiled.Events.Handlers.Player.Died -= Player_Died;
            Exiled.Events.Handlers.Player.Verified -= Player_Verified;
            Exiled.Events.Handlers.Player.Destroying -= Player_Left;
            Exiled.Events.Handlers.Player.Kicking -= Player_Kicking;
            Exiled.Events.Handlers.Player.Banning -= Player_Banning;
            Exiled.Events.Handlers.Player.ThrownProjectile -= Player_ThrowingItem;
            Exiled.Events.Handlers.Server.WaitingForPlayers -= Server_WaitingForPlayers;
            RgCore.RoundStarted -= Server_RoundStarted;
            Exiled.Events.Handlers.Server.RoundEnded -= Server_RoundEnded;
            Exiled.Events.Handlers.Warhead.Starting -= Warhead_Starting;
            Exiled.Events.Handlers.Warhead.Stopping -= Warhead_Stopping;
            Exiled.Events.Handlers.Warhead.Detonated -= Warhead_Detonated;
            Exiled.Events.Handlers.Server.RespawningTeam -= Server_RespawningTeam;
        }
    }
}